package cn.csust.net2.manager.server.servlet;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.csust.net2.manager.server.security.AuthCurrentUser;
import cn.csust.net2.manager.server.service.impl.BaseServiceImpl;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.FileNoticService;
import cn.csust.net2.manager.shared.util.ServiceConstant;
import cn.csust.net2.manager.shared.util.Utils;

public class UpLoadFileNotification extends HttpServlet{
	/**
	 * 
	 */
	//字符编码

	 private final String ENCODING="UTF-8";
	private static final long serialVersionUID = -3907176325780019288L;
	private void readParams(HttpServletRequest req,HttpServletResponse resp,List<FileItem> items) throws UnsupportedEncodingException{
		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		User user = (User)req.getSession().getAttribute(BaseServiceImpl.USER_SYS);
		AuthCurrentUser.set(user);
		//设置request对象的字符编码

		req.setCharacterEncoding(ENCODING);
		FileNoticService service =(FileNoticService) applicationContext.getBean(ServiceConstant.SERVICE_NAME_FILENOTIC);
		try {
		//	cn.csust.net2.manager.shared.po.FileLoadNotification fntr = new cn.csust.net2.manager.shared.po.FileLoadNotification();
//			fntr.setAcademy(((Teacher)(user)).getAcademy());
//			fntr.setFileNotification(fileNotification);
			String id = null;
			FileItem file = null;
			for (final FileItem item : items) {
				if(item.isFormField()){
					System.out.println(item.getString());
					String itemName = item.getFieldName();
					if(itemName==null)
						itemName="";
					if(itemName.equals("id")){
						id = item.getString();
					}
				}
				if (!item.isFormField()) {
					file = item;			
					
				}
				
			}
			String fileName = file.getName();
			String fileSuffix = Utils.getSuffix(fileName);
			Integer ok = service.saveUpLoadFileNotic(fileName, fileSuffix, id);
			
			if(ok!=null){
				
				
			    fileName = "";
				fileName =ok+ "." + fileSuffix;
				String  filePath=this.getServletConfig().getServletContext().getRealPath("/");
				file.write(new File(filePath+"\\files\\upload\\", fileName));
				resp.getWriter().write(fileName);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
	public String getUTF(String org) throws UnsupportedEncodingException{
		 String x = new String(org.getBytes("ISO-8859-1"), ENCODING);
		 return x;
		 
	}
//	private String[] image  = {"jpg", "png", "gif", "bmp"};
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> items = upload.parseRequest(req);
			this.readParams(req, resp, items);
		} catch (FileUploadException e) {
			
			e.printStackTrace();
		}
		
		
	}
	
//	/**
//	 * 根据上传文件的后缀判断改文件是否是图片文件
//	 * @param fileStuffix
//	 * @return
//	 */
//	private boolean isImageFile(String fileStuffix) {
//		String stuffix = fileStuffix.toLowerCase();
//		for(int i=0; i<image.length; i++) {
//			if(stuffix.equals(image[i])) {
//				return true;
//			}
//		}
//		return false;
//	}
}
